/** Description of NumberComparator.java 
 * @version 1.0
 * 
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.logical;

import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;

public final class NumberComparator implements Comparator<Number> {
	
	private static NumberComparator comparatorInstance = null;
	
	private NumberComparator() {
		
	}
	
	public static NumberComparator getInstance() {
		if(comparatorInstance == null) {
			comparatorInstance = new NumberComparator();
		}
		return comparatorInstance;
	}

	public int compare(Number object1, Number object2) {
		if(object1.floatValue() > object2.floatValue()) {
			return 1;
		} else if(object1.floatValue() < object2.floatValue()) {
			return -1;
		} else {
			return 0;
		}
	}
	
	public Long daysBetween(Date oldDate, Date newDate) {
		//TODO Nullify time first before returning
		return (newDate.getTime() - oldDate.getTime()) / (1000 * 60 * 60 * 24);
	}
	
	public Date getDayStartingDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 1);
		return cal.getTime();
	}
	
	public Date getDayEndingDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}
	
	public Date getWeekStartingDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 1);
		return cal.getTime();
	}
	
	public Date getWeekEndingDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}
	
	public Date[] getStartingAndEndingWeekDate(Date date) {
		Date[] dateArr = new Date[2];
		dateArr[0] = getWeekStartingDate(date);
		dateArr[1] = getWeekEndingDate(date);
		return dateArr;
	}

	public boolean isDatesInSameMonth(Date oldDate, Date newDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(oldDate);
		int oldMonth = cal.get(Calendar.MONTH);
		int oldYear = cal.get(Calendar.YEAR);
		cal.setTime(newDate);
		int newMonth = cal.get(Calendar.MONTH);
		int newYear = cal.get(Calendar.YEAR);
		return ((oldMonth == newMonth) && (oldYear == newYear)) ? true:false;
	}
	
	public Date getMonthStartingDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 1);
		return cal.getTime();
	}
	
	public Date getMonthEndingDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}
	
	public Date[] getStartingAndEndingMonthDate(Date date) {
		Date[] dateArr = new Date[2];
		dateArr[0] = getMonthStartingDate(date);
		dateArr[1] = getMonthEndingDate(date);
		return dateArr;
	}
	
	public Date getWeekFortnightStartingDate(Date date) {
		Date weekStartingDate = getWeekStartingDate(date);
		Long newTimeMilli = weekStartingDate.getTime() - (14 * 1000 * 60 * 60 * 24);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(newTimeMilli);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 1);
		return cal.getTime();
	}
	
}